//
//  RepaircadeViewController.m
//  ICrEAM
//
//  Created by mac on 16/10/31.
//  Copyright © 2016年 mac. All rights reserved.
//

#import "RepaircadeViewController.h"
#import "RepairCadeCell.h"
#import "repairHeadView.h"
#import "EXTScope.h"
#import "RepaircadeResponse.h"
#import "RepaircadeAction.h"
@interface RepaircadeViewController (){
    NSString *_searchStr;//切换类型
    NSString *_type; //排序类型
    NSInteger _page;
    NSInteger _prePage;
}

//维修进度看板数组
@property(nonatomic,strong)NSMutableArray *allrepairs;


@end

@implementation RepaircadeViewController
-(NSMutableArray *)allrepairs{
    if (!_allrepairs) {
        _allrepairs=[NSMutableArray array];
    }
    return _allrepairs;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    [self setupNav];
    self.title=@"维修进度看板";
    // Do any additional setup after loading the view from its nib.
}

-(void)setupNav{
    UIView *view=[[UIView alloc]initWithFrame:CGRectMake(0, 0, ScreenWidth, 44)];
    view.backgroundColor=RGB(230, 236, 236);
    self.searchTextField.frame=CGRectMake(ScreenWidth-380, 4, 340, 36);
    self.searchTextField.placeholder=@"车牌号、送修人姓名、送修人手机、服务顾问";
    [view addSubview:self.searchTextField];
    self.switchArray=@[@"当天",@"3天内"];
    self.mySwitch.frame=CGRectMake(0,0,300,44);
    [view addSubview: self.mySwitch];
    [self.view addSubview:view];
    [ self.mySwitch selectIndex:0 animated:YES];
    @weakify(self);
    [ self.mySwitch setWillBePressedHandler:^(NSUInteger index) {
        @strongify(self);
        if (index==0) {
            _searchStr=@"";
        }else{
            _searchStr=@"THREE";
        }
        [self.tableView.mj_header beginRefreshing];
        
    }];
    //添加tableview的标题栏
    repairHeadView *head=[repairHeadView createRepairHeadView];
    if (!IsStrEmpty(_type)) {
        head.selectType=[_type integerValue];
    }
    head.orderBlock=^(NSInteger type){
        @strongify(self);
        _type=[NSString stringWithFormat:@"%d",type];
        [self.tableView.mj_header beginRefreshing];
    };
    head.translatesAutoresizingMaskIntoConstraints=NO;
    UIView *sectionView=[[UIView alloc]initWithFrame:CGRectMake(0, CGRectGetMaxY(view.frame), ScreenWidth, 48)];
    
    [sectionView addSubview:head];
    [head mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.equalTo(sectionView.mas_leading).with.offset(0);
        make.top.equalTo(sectionView.mas_top).with.offset(1);
        make.bottom.equalTo(sectionView.mas_bottom).with.offset(1);
        make.trailing.equalTo(sectionView.mas_trailing).with.offset(0);
    }];
    UIView *linew=[[UIView alloc]initWithFrame:CGRectMake(0, 47, ScreenWidth, 1)];
    linew.backgroundColor=RGB(204, 204, 204);
    [sectionView addSubview:linew];
    UIView *linew1=[[UIView alloc]initWithFrame:CGRectMake(0,0, ScreenWidth, 1)];
    linew1.backgroundColor=RGB(204, 204, 204);
    [sectionView addSubview:linew1];
    [self.view addSubview:sectionView];
    //设置tableview
    self.tableView.frame=CGRectMake(0, CGRectGetMaxY(sectionView.frame), ScreenWidth, ScreenHeight-158);
    self.tableView.separatorStyle=UITableViewCellSeparatorStyleNone;
    [self.view addSubview:self.tableView];
    self.tableView.mj_header=[MJRefreshNormalHeader headerWithRefreshingBlock:^{
        _prePage=_page;
        _page=1;
        [self requestDataIsHead:YES];
    }];
    self.tableView.mj_footer=[MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        _prePage=_page;
        _page++;
        [self requestDataIsHead:NO];
    }];
    [self.tableView.mj_header beginRefreshing];
    
}
#pragma 搜索框delegate
- (BOOL)textFieldShouldReturn:(UITextField *)textField{
    [self.tableView.mj_header beginRefreshing];
    [textField resignFirstResponder];
    return YES;
}

/**
 *  获取维修进度看板数据
 *
 **/
-(void)requestDataIsHead:(BOOL)ishead{
    
    RepaircadeAction *act=[[RepaircadeAction alloc]init];
    act.page=[NSString stringWithFormat:@"%d",_page];
    act.query_date=_searchStr;
    if (!IsStrEmpty(self.searchTextField.text)) {
        act.license=self.searchTextField.text;
    }
    if (!IsStrEmpty(_type)) {
        act.order=_type;
    }
    [self requestDataWithAction:act haveHud:NO showError:NO  success:^(__kindof YTKBaseRequest *request) {
        NSDictionary *resDic=request.responseJSONObject;
        NSArray *arr=resDic[@"TT_REPAIR_ORDER"];
        if (ishead) {
            [self.allrepairs removeAllObjects];
        }        
        if ([arr isKindOfClass:[NSArray class]]) {
            if (!IsArrEmpty(arr)) {
                for (NSDictionary *dic in arr) {
                    RepaircadeResponse *model=[RepaircadeResponse mj_objectWithKeyValues:dic];
                    [self.allrepairs addObject:model];
                }
            }else{
                [self.tableView.mj_footer endRefreshingWithNoMoreData];
                [self showText:@"没有更多结果"];
            }
        }else{
            [self.tableView.mj_footer endRefreshingWithNoMoreData];
            [self showText:@"没有更多结果"];
        }
        [self.tableView reloadData];
        [self.tableView.mj_header endRefreshing];
        [self.tableView.mj_footer endRefreshing];;
    } failure:^(__kindof YTKBaseRequest *request) {
        _page=_prePage;
        [self.tableView.mj_footer endRefreshing];;
        [self.tableView.mj_header endRefreshing];
    }];
    
}
#pragma mark  UITableViewDelegate
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return 90;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return self.allrepairs.count;
    
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    RepairCadeCell *cell=[RepairCadeCell createRepairCadeCell];
    if (indexPath.row==0) {
        UIView *topline=[[UIView alloc]initWithFrame:CGRectMake(0, 0, ScreenWidth, 1)];
        topline.backgroundColor=RGB(240, 240, 240);
        [cell.contentView addSubview:topline];
    }
    if (indexPath.row==self.allrepairs.count-1) {
        UIView *linew=[[UIView alloc]initWithFrame:CGRectMake(0, 89, ScreenWidth, 1)];
        linew.backgroundColor=RGB(240, 240, 240);
        [cell.contentView addSubview:linew];
    }
    
    RepaircadeResponse *model=self.allrepairs[indexPath.row];
    cell.repaircadeModel=model;
    if (indexPath.row%2!=0) {
        
        cell.backgroundColor=RGB(247, 247, 247);
    }
    
    return cell;
    
}


@end
